<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('节假日配置') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="auto">
      <el-form-item :label="$t('节假日名称')" prop="name">
        <el-input
          v-model="queryParams.name"
          size="small"
          :placeholder="$t('节假日名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('节假日日期')" prop="vacationsDate">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:holiday:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:holiday:upload']"
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="upload.open = true"
        >{{ $t('导入') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:holiday:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="oaVacationHolidayList" border>
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column :label="$t('节假日名称')" align="center" prop="name" show-overflow-tooltip />
      <el-table-column :label="$t('日期')" align="center" prop="vacationsDate" show-overflow-tooltip width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.vacationsDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('备注')" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['oa:holiday:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['oa:holiday:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改节假日配置对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" width="50%" :close-on-click-modal="false" :lock-scroll="false">
      <oaVacationHolidayForm ref="oaVacationHolidayForm" />
    </el-dialog>

    <!-- 导入窗口 -->
    <el-dialog :title="$t('导入')" :visible.sync="upload.open" width="400px" :close-on-click-modal="false" :lock-scroll="false" append-to-body>
      <div>
        <el-upload
          ref="upload"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :on-exceed="handleFileExceed"
          :auto-upload="false"
          drag
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text" v-html="$t('将文件拖到此处，或<em>点击上传</em>')" />
          <div slot="tip" class="el-upload__tip" style="color:red">
            {{ $t('提示：仅允许导入“xls”或“xlsx”格式文件！') }}
            <el-link type="primary" style="font-size:12px" @click="fetchTemplate">
              {{ $t('下载模板') }}
            </el-link>
          </div>
        </el-upload>

        <div slot="footer" class="dialog-footer" style="text-align: right;">
          <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
          <el-button type="primary" @click="submitFileForm">{{ $t("确定") }}</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOaVacationHoliday,
  delOaVacationHoliday
} from '@/api/oa/vacation/holiday/oaVacationHoliday'
import oaVacationHolidayForm from './oaVacationHolidayForm.vue'
import { getToken } from '@/utils/auth'
export default {
  components: {
    oaVacationHolidayForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 节假日配置表格数据
      oaVacationHolidayList: [],
      // 弹出层标题
      title: '节假日配置',
      // 是否显示弹出层
      formOpen: false,
      // 查询时间范围
      dateRange: [],
      // 导入参数
      upload: {
        // 是否显示弹出层（项目导入）
        open: false,
        // 弹出层标题（项目导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/vacation/oaVacationHoliday/importData'
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        vacationsDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询节假日配置列表 */
    getList() {
      this.loading = true
      listOaVacationHoliday(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.oaVacationHolidayList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        vacationsDate: undefined,
        remark: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = undefined
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.oaVacationHolidayForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.oaVacationHolidayForm.init(row.id)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(function() {
          return delOaVacationHoliday(row.id)
        })
        .then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
        })
        .catch(function() {})
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/vacation/oaVacationHoliday/export', this.addDateRange(this.queryParams, this.dateRange))
    },
    // 文件上传中处理
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      if (response.msg === 'success') {
        this.msgSuccess(this.$t('导入成功'))
      } else if (response.code === 500) {
        this.msgError(response.msg)
      } else {
        this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
      }
      // this.$alert(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    // 文件达到上限处理
    handleFileExceed() {
      this.msgError(this.$t('可上传文件数量已达到上限'))
    },
    // 下载模板
    fetchTemplate() {
      var href1 = window.location.href
      var href2 = this.$route.path
      var url = href1.substring(0, href1.indexOf(href2))
      window.location.href = url + '/template/holidaySettingImportTemplate.xlsx'
    }
  }
}
</script>
